package com.seeyon.apps.u8cloud.manager;

import com.aspose.imaging.internal.hC.iF;
import com.seeyon.apps.bense.util.CAP4FormKit;
import com.seeyon.apps.bense.util.SaveToUnFlowUtil;
import com.seeyon.apps.common.utils.CAP4FormFieldUtils;
import com.seeyon.apps.u8cloud.model.CommonResult;
import com.seeyon.apps.u8cloud.model.Details;
import com.seeyon.apps.u8cloud.model.ParamAss;
import com.seeyon.apps.u8cloud.model.ResultData;
import com.seeyon.apps.u8cloud.model.Voucher;
import com.seeyon.apps.u8cloud.model.VoucherParams;
import com.seeyon.apps.u8cloud.model.VoucherRecord;
import com.seeyon.apps.u8cloud.model.VoucherResult;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormDataSubBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.affair.strategy.newsStrategy;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.util.annotation.Inject;
import com.seeyon.ctp.util.json.JSONUtil;
import org.apache.commons.logging.Log;
import org.apache.logging.log4j.util.Strings;
import org.apache.poi.ss.util.SSCellRange;
import org.apache.xalan.xsltc.compiler.sym;

import www.seeyon.com.utils.DateUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName : U8CloudVoucherManagerImpl
 * @Description :
 * @CreateDate : 2025/9/2 10:09
 * @Author : CheungHowrAn
 * @Version : 1.0.0
 * @PackageName com.seeyon.apps.u8cloud.manager
 * @Project : BSiEE本涩_A8_V9.0SP1
 */
public class U8CloudVoucherManagerImpl implements U8CloudVoucherManager {
	private static final Log log = CtpLogFactory.getLog(U8CloudVoucherManager.class);
	@Inject
	private FormApi4Cap4 formApi4Cap4;
	@Inject
	private U8CloudApiManager u8CloudApiManager;
	@Inject
	private VoucherRecordManager voucherRecordManager;
	private static String serviceUrl = AppContext.getSystemProperty("u8cloud.serviceUrl");
	private	Integer no = 1;
	/**
	 * @MethodName: createVoucher
	 * @Description: 创建凭证--档案表按钮
	 * @CreateDate: 2025/9/2 14:09
	 * @Author: CheungHowrAn
	 * @Param formId
	 * @Param idStrList
	 * @Return CommonResult
	 */
	public synchronized  CommonResult createVoucher(String formId, List<String> idStrList) {
		CommonResult result = new CommonResult();
		VoucherParams voucherParams = new VoucherParams();
		VoucherResult voucherResult = new VoucherResult();
		try {
			FormBean form = formApi4Cap4.getForm(Long.valueOf(formId));
			log.info("form==="+form); 
//			String formCode = form.getBind().getFormCode();
//			List<VoucherParams> paramsList = new ArrayList<>();
			List<FormDataMasterBean> payMasterList = new ArrayList<FormDataMasterBean>();
			List<FormDataMasterBean> expenseMasterList = new ArrayList<FormDataMasterBean>();
			List<FormDataMasterBean> transMasterList = new ArrayList<FormDataMasterBean>();
			log.info("idStrList==="+idStrList); 
			for (String idStr : idStrList) {
				FormDataMasterBean dataMasterBean = formApi4Cap4.getDataMasterBeanById(Long.valueOf(idStr), form, null);
				log.info("dataMasterBean==="+dataMasterBean); 
				String billType = CAP4FormKit.getFieldStrValue(dataMasterBean, "单据类型");
				log.info("单据类型==="+billType); 
				if ("付款单".equals(billType)) {
					payMasterList.add(dataMasterBean);
				} else if ("费用报销单".equals(billType)) {
					expenseMasterList.add(dataMasterBean);
				} else if ("差旅报销单".equals(billType)) {
					transMasterList.add(dataMasterBean);
				}
			}
			log.info("payMasterList==="+payMasterList); 
			log.info("expenseMasterList==="+expenseMasterList); 
			log.info("transMasterList==="+transMasterList); 
			// 组装对应的分录-借
			
			List<Details> paymentDetails = getZonZhangPaymentDetails(payMasterList,  "付款单-");
			log.info("paymentDetails==="+paymentDetails); 
			log.info("no==="+no); 
			List<Details> expenseDetails = getZonZhangExpenseDetails(expenseMasterList, "费用-");
			log.info("expenseDetails==="+expenseDetails); 
			log.info("no==="+no); 
			List<Details> transDetails = getZonZhangTransDetails(transMasterList,  "差旅费-");
			log.info("transDetails==="+transDetails); 
			log.info("no==="+no); 
			// 组装对应的分录-贷
			List<Details> daiList = getZonZhangDaiDetails(idStrList,form);
			log.info("daiList==="+daiList); 
			log.info("no==="+no); 
			no = 1;
			// 凭证头部
			voucherParams = getZonzhangVoucher(idStrList.get(0), form,paymentDetails,expenseDetails,transDetails,daiList);
			log.info("voucherParams==="+voucherParams); 
			voucherResult = u8CloudApiManager.createVoucher(voucherParams);
			if (voucherResult.getStatus().equals("success")) {
				result.setSuccess(true);
				List<ResultData> resultData = voucherResult.getResultData();
				// todo 凭证号
				String pkVoucher = resultData.get(0).getNo();
//				master.addFieldValue(form.getFieldBeanByDisplay("回传凭证号").getName(), pkVoucher);
				// 更新档案表数据
				Map<String, Object> paramMap = new HashMap<>();
				paramMap.put("凭证号", pkVoucher);
				for (String idStr : idStrList) {
					FormDataMasterBean dataMasterBean = formApi4Cap4.getDataMasterBeanById(Long.valueOf(idStr), form, null);
					SaveToUnFlowUtil.updateFormData(paramMap, dataMasterBean, form, true);
				}
				//返回对应参数
				result.setData(pkVoucher);
				result.setMessage("凭证生成成功，凭证号为：" + pkVoucher);
			} else {
				result.setSuccess(false);
				result.setMessage(voucherResult.getErrormsg());
			}
			
		} catch (Exception e) {
			log.error("获取付款凭证参数失败", e);
			result.setSuccess(false);
			result.setMessage("凭证生成失败");
		}

		// 记录日志
		VoucherRecord voucherRecord = new VoucherRecord();
		voucherRecord.setDataId(JSONUtil.toJSONString(idStrList));
		voucherRecord.setFormId(formId);
		voucherRecord.setSubject("");
		if (voucherResult.getStatus().equals("success")) {
			voucherRecord.setVoucherId(voucherResult.getResultData().get(0).getPk_voucher());
			voucherRecord.setVoucherCode(voucherResult.getResultData().get(0).getNo());
		}
		voucherRecord.setStatus(String.valueOf(result.getSuccess()));
		voucherRecord.setServiceUrl(serviceUrl + "/u8cloud/api/gl/voucher/insert");
		voucherRecord.setParams(JSONUtil.toJSONString(voucherParams));
		voucherRecord.setResult(JSONUtil.toJSONString(voucherResult));
		voucherRecord.setCreateDate(DateUtil.get19DateAndTime());
		voucherRecordManager.saveDockingRecords(voucherRecord);
		return result;
	}

	private List<Details> getZonZhangDaiDetails(List<String> idStrList, FormBean form) {
		// TODO Auto-generated method stub
		List<Details> detailsList = new ArrayList<>();
		List<Map<String,String>> zhifufeiyon = new ArrayList<>();
		List<Map<String,String>> yinhangkemu = new ArrayList<>();
		List<Map<String,String>> yinhangName = new ArrayList<>();
        LinkedHashSet<String> set = new LinkedHashSet<>();// 重复的元素不会添加
		try {
			for (String idStr : idStrList) {
				FormDataMasterBean dataMasterBean = formApi4Cap4.getDataMasterBeanById(Long.valueOf(idStr), form, null);
				String billType = CAP4FormKit.getFieldStrValue(dataMasterBean, "单据类型");
				if ("付款单".equals(billType)) {
					Map<String,String> map = new HashMap<String, String>();
					Map<String,String> kemuMap = new HashMap<String, String>();
					Map<String,String> nameMap = new HashMap<String, String>();
					String jine = CAP4FormKit.getFieldStrValue(dataMasterBean, "付款单-4.1本次支付金额");
					String zhanghao = CAP4FormKit.getFieldStrValue(dataMasterBean, "付款单-付款银行账号");
					String kemu = CAP4FormKit.getFieldStrValue(dataMasterBean, "付款单-总账贷方科目编码");
					map.put(zhanghao, jine);
					set.add(zhanghao);
					zhifufeiyon.add(map);
					kemuMap.put(zhanghao, kemu);
					yinhangkemu.add(kemuMap);
					String yhName = CAP4FormKit.getFieldStrValue(dataMasterBean, "付款单-账户名称");
					nameMap.put(zhanghao, yhName);
					yinhangName.add(nameMap);
				} else if ("费用报销单".equals(billType)) {
					Map<String,String> map = new HashMap<String, String>();
					Map<String,String> kemuMap = new HashMap<String, String>();
					Map<String,String> nameMap = new HashMap<String, String>();
					String zhanghao = CAP4FormKit.getFieldStrValue(dataMasterBean, "费用-付款银行账号");
					String jine = CAP4FormKit.getFieldStrValue(dataMasterBean, "费用-3.6本次支付金额");
					String kemu = CAP4FormKit.getFieldStrValue(dataMasterBean, "费用-总账贷方科目编码");
					map.put(zhanghao, jine);
					set.add(zhanghao);
					zhifufeiyon.add(map);
					kemuMap.put(zhanghao, kemu);
					yinhangkemu.add(kemuMap);
					String yhName = CAP4FormKit.getFieldStrValue(dataMasterBean, "费用-账户名称");
					nameMap.put(zhanghao, yhName);
					yinhangName.add(nameMap);
				} else if ("差旅报销单".equals(billType)) {
					Map<String,String> map = new HashMap<String, String>();
					Map<String,String> kemuMap = new HashMap<String, String>();
					Map<String,String> nameMap = new HashMap<String, String>();
					String zhanghao = CAP4FormKit.getFieldStrValue(dataMasterBean, "差旅费-付款银行账号");
					String jine = CAP4FormKit.getFieldStrValue(dataMasterBean, "差旅费-3.13本次支付金额");
					String kemu = CAP4FormKit.getFieldStrValue(dataMasterBean, "差旅费-总账贷方科目编码");
					map.put(zhanghao, jine);
					set.add(zhanghao);
					zhifufeiyon.add(map);
					kemuMap.put(zhanghao, kemu);
					yinhangkemu.add(kemuMap);
					String yhName = CAP4FormKit.getFieldStrValue(dataMasterBean, "差旅费-账户名称");
					nameMap.put(zhanghao, yhName);
					yinhangName.add(nameMap);
				}
			}
			
			List<String> zhanghaoList = new ArrayList<>(set); // 将Set转换为List
			
			log.info("zhanghaoList==="+zhanghaoList); 
			log.info("yinhangkemu==="+yinhangkemu); 
			log.info("zhifufeiyon==="+zhifufeiyon); 
			log.info("yinhangName==="+yinhangName); 
			log.info("no==="+no); 
			for (int i = 0; i < zhanghaoList.size(); i++) {
				String zhanghao = zhanghaoList.get(i);
				String kemu = "";
				String yhName = "";
				BigDecimal feiyon = BigDecimal.ZERO;
				for (int j = 0; j < zhifufeiyon.size(); j++) {
					if(zhifufeiyon.get(j).containsKey(zhanghao)){
						log.info("new BigDecimal(zhifufeiyon.get(j).get(zhanghao))==="+new BigDecimal(zhifufeiyon.get(j).get(zhanghao))); 
						feiyon = feiyon.add(new BigDecimal(zhifufeiyon.get(j).get(zhanghao)));
					}
					log.info("feiyon==="+feiyon); 
				}
				
				for (int j = 0; j < yinhangkemu.size(); j++) {
					if(yinhangkemu.get(j).containsKey(zhanghao)){
						kemu = yinhangkemu.get(j).get(zhanghao);
						break;
					}
				}
				
				for (int j = 0; j < yinhangName.size(); j++) {
					if(yinhangName.get(j).containsKey(zhanghao)){
						yhName = yinhangName.get(j).get(zhanghao);
						break;
					}
				}
				
				log.info("kemu==="+kemu); 
				log.info("zhanghao==="+zhanghao); 
				log.info("feiyon==="+feiyon); 
				log.info("yhName==="+yhName); 
				//贷方
				// 贷：库存现金 / 银行存款 5,000.00
				Details bankDepositDetails = new Details();
				// 原币贷方金额
				bankDepositDetails.setCreditamount(feiyon.toString());
				// 如果没有外币业务情况可以跟creditamount传输一样值
				bankDepositDetails.setLocalcreditamount(feiyon.toString());
				// 原币借方金额
				bankDepositDetails.setDebitamount("0");
				// 如果没有外币业务情况可以传debitamount一样
				bankDepositDetails.setLocaldebitamount("0");
				// 分录行也可以理解为行号从1开始排
				bankDepositDetails.setDetailindex(no.toString());
				// 摘要信息
				bankDepositDetails.setExplanation(yhName);
				// 科目代码
				bankDepositDetails.setPk_accsubj(kemu);//10020201

				// 辅助科目*********
//				ParamAss bankDepositAss = new ParamAss();
//				bankDepositAss.setChecktypecode("96");// 银行
//				bankDepositAss.setCheckvaluecode(zhanghao);
//				List<ParamAss> bankDepositAssList = new ArrayList<>();
//				bankDepositAssList.add(bankDepositAss);
//				bankDepositDetails.setAss(bankDepositAssList);
				
				detailsList.add(bankDepositDetails);
				no++;
			}
			
		} catch (NumberFormatException | BusinessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return detailsList;
	}

	private List<Details> getZonZhangTransDetails(List<FormDataMasterBean> transMasterList, String un_) {
		// TODO Auto-generated method stub
		List<Details> detailsList = new ArrayList<>();
		try {
			for (int i = 0; i < transMasterList.size(); i++) {
				FormDataMasterBean master = transMasterList.get(i);
				String costItem = CAP4FormFieldUtils.getFormFieldVal(master, un_+"费用项",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
				log.info("费用项：" + costItem);

				// 交通费合计
				String jtwshj = CAP4FormKit.getFieldStrValue(master, un_+"交通未税金额合计");
				String bz = CAP4FormKit.getFieldStrValue(master, un_+"借方摘要");
				if (Strings.isNotBlank(jtwshj) && new BigDecimal(jtwshj).compareTo(BigDecimal.ZERO) > 0) {
					// 借：管理费用 — 费用费
					Details travelExpenseDetails = new Details();
					// 原币贷方金额
					travelExpenseDetails.setCreditamount("0");
					// 如果没有外币业务情况可以跟creditamount传输一样值
					travelExpenseDetails.setLocalcreditamount("0");
					// 原币借方金额
					travelExpenseDetails.setDebitamount(jtwshj);
					// 如果没有外币业务情况可以传debitamount一样
					travelExpenseDetails.setLocaldebitamount(jtwshj);
					// 分录行也可以理解为行号从1开始排
					travelExpenseDetails.setDetailindex(no.toString());
					// 摘要信息
					travelExpenseDetails.setExplanation(bz);
					// 科目代码--城市间交通费
					if ("管理费用".equals(costItem)) {
						travelExpenseDetails.setPk_accsubj("66020501");
					} else {
						travelExpenseDetails.setPk_accsubj("66010801");
					}

					// 辅助科目*********用的是自定义字段--"门店"
					ParamAss ass = new ParamAss();
					ass.setChecktypecode("2");
					ass.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, un_+"部门编码-关联",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
					List<ParamAss> assList = new ArrayList<>();
					assList.add(ass);
					travelExpenseDetails.setAss(assList);

					detailsList.add(travelExpenseDetails);
					no++;
				}
				// 住宿费合计
				String zswshj = CAP4FormKit.getFieldStrValue(master, un_+"住宿未税金额合计");
				if (Strings.isNotBlank(zswshj) && new BigDecimal(zswshj).compareTo(BigDecimal.ZERO) > 0) {
					// 借：管理费用 — 费用费
					Details zhusuExpenseDetails = new Details();
					// 原币贷方金额
					zhusuExpenseDetails.setCreditamount("0");
					// 如果没有外币业务情况可以跟creditamount传输一样值
					zhusuExpenseDetails.setLocalcreditamount("0");
					// 原币借方金额
					zhusuExpenseDetails.setDebitamount(zswshj);
					// 如果没有外币业务情况可以传debitamount一样
					zhusuExpenseDetails.setLocaldebitamount(zswshj);
					// 分录行也可以理解为行号从1开始排
					zhusuExpenseDetails.setDetailindex(no.toString());
					// 摘要信息
					zhusuExpenseDetails.setExplanation(bz);
					// 科目代码--住宿费
					if ("管理费用".equals(costItem)) {
						zhusuExpenseDetails.setPk_accsubj("66020502");
					} else {
						zhusuExpenseDetails.setPk_accsubj("66010802");
					}

					// 辅助科目*********用的是自定义字段--"门店"
					ParamAss ass = new ParamAss();
					ass.setChecktypecode("2");
					ass.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, un_+"部门编码-关联",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
					List<ParamAss> assList = new ArrayList<>();
					assList.add(ass);
					zhusuExpenseDetails.setAss(assList);

					detailsList.add(zhusuExpenseDetails);
					no++;
				}

				// 出差补助合计
				String bzhj = CAP4FormKit.getFieldStrValue(master, un_+"3.5出差补助合计");
				if (Strings.isNotBlank(bzhj) && new BigDecimal(bzhj).compareTo(BigDecimal.ZERO) > 0) {
					// 借：管理费用 — 费用费
					Details buzhuExpenseDetails = new Details();
					// 原币贷方金额
					buzhuExpenseDetails.setCreditamount("0");
					// 如果没有外币业务情况可以跟creditamount传输一样值
					buzhuExpenseDetails.setLocalcreditamount("0");
					// 原币借方金额
					buzhuExpenseDetails.setDebitamount(bzhj);
					// 如果没有外币业务情况可以传debitamount一样
					buzhuExpenseDetails.setLocaldebitamount(bzhj);
					// 分录行也可以理解为行号从1开始排
					buzhuExpenseDetails.setDetailindex(no.toString());
					// 摘要信息
					buzhuExpenseDetails.setExplanation(bz);
					// 科目代码--综合补贴
					if ("管理费用".equals(costItem)) {
						buzhuExpenseDetails.setPk_accsubj("66020503");
					} else {
						buzhuExpenseDetails.setPk_accsubj("66010803");
					}

					// 辅助科目*********用的是自定义字段--"门店"
					ParamAss ass = new ParamAss();
					ass.setChecktypecode("2");
					ass.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, un_+"部门编码-关联",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
					List<ParamAss> assList = new ArrayList<>();
					assList.add(ass);
					buzhuExpenseDetails.setAss(assList);

					detailsList.add(buzhuExpenseDetails);
					no++;
				}

//				// 税额合计
//				BigDecimal jttaxRate = new BigDecimal(jtsehj);
//				BigDecimal zsRate = new BigDecimal(zssehj);
//				BigDecimal taxTotle = jttaxRate.add(zsRate);
//				if (taxTotle != null && taxTotle.compareTo(BigDecimal.ZERO) > 0) {
//					// 借：应交税费 — 应交增值税(进项税额) 283.02
//					Details taxesPayableDetails = new Details();
//					// 原币贷方金额
//					taxesPayableDetails.setCreditamount("0");
//					// 如果没有外币业务情况可以跟creditamount传输一样值
//					taxesPayableDetails.setLocalcreditamount("0");
//					// 原币借方金额
//					taxesPayableDetails.setDebitamount(taxTotle.toString());
//					// 如果没有外币业务情况可以传debitamount一样
//					taxesPayableDetails.setLocaldebitamount(taxTotle.toString());
//					// 分录行也可以理解为行号从1开始排
//					taxesPayableDetails.setDetailindex(no.toString());
//					// 摘要信息
//					taxesPayableDetails.setExplanation(bz);
//					// 科目代码
//					taxesPayableDetails.setPk_accsubj("222111");
//					detailsList.add(taxesPayableDetails);
//					no++;
//				}
			}
		} catch (Exception e) {
			log.error("差旅报销单凭证生成事件异常", e);
		}
		return detailsList;
	}

	private List<Details> getZonZhangExpenseDetails(List<FormDataMasterBean> expenseMasterList,
			String un_) {
		// TODO Auto-generated method stub
		List<Details> detailsList = new ArrayList<>();
		try {
			for (int i = 0; i < expenseMasterList.size(); i++) {
				FormDataMasterBean master = expenseMasterList.get(i);
				List<FormDataSubBean> subData = CAP4FormKit.getSubBeans(master, "费用报销明细");
				String costItem = CAP4FormFieldUtils.getFormFieldVal(master, un_ + "费用项",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
				for (FormDataSubBean subBean : subData) {

					// 无借款 报销5000
					// 借：管理费用 — 费用费 4,716.98
					Details travelExpenseDetails = new Details();
					// 原币贷方金额
					travelExpenseDetails.setCreditamount("0");
					// 如果没有外币业务情况可以跟creditamount传输一样值
					travelExpenseDetails.setLocalcreditamount("0");
					// 原币借方金额
					travelExpenseDetails.setDebitamount(CAP4FormFieldUtils.getFormFieldVal(subBean, un_ + "L1.5费用金额",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
					// 如果没有外币业务情况可以传debitamount一样
					travelExpenseDetails.setLocaldebitamount(CAP4FormFieldUtils.getFormFieldVal(subBean,
							un_ + "L1.5费用金额", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
					// 分录行也可以理解为行号从1开始排
					travelExpenseDetails.setDetailindex(no.toString());
					// 摘要信息
					travelExpenseDetails.setExplanation(CAP4FormFieldUtils.getFormFieldVal(subBean, un_ + "借方摘要",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
					// 科目代码
					if ("管理费用".equals(costItem)) {
						travelExpenseDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(subBean,
								un_ + "管理对应U8科目代码", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
					} else {
						travelExpenseDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(subBean,
								un_ + "财务销售U8科目代码", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
					}
					
					String checkAss = CAP4FormFieldUtils.getFormFieldVal(subBean, un_ + "是否有辅助核算",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
					if ("是".equals(checkAss)) {
						// 辅助科目*********用的是自定义字段--"门店"
						ParamAss ass = new ParamAss();
						ass.setChecktypecode("2");
						ass.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, un_+"部门编码-关联",
								CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
						List<ParamAss> assList = new ArrayList<>();
						assList.add(ass);
						travelExpenseDetails.setAss(assList);
					}

					detailsList.add(travelExpenseDetails);
					no++;
				}
			}
		} catch (Exception e) {
			log.error("总账费用凭证分录生成事件异常", e);
		}
		return detailsList;
	}

	/**
	 * @param un_
	 * @MethodName: getPaymentVoucherParams
	 * @Description: 获取付款总账凭证借方参数
	 * @CreateDate: 2025/9/2 14:09
	 * @Author: CheungHowrAn
	 * @Param master
	 * @Param formBean
	 * @Return List<Details>
	 */
	public List<Details> getZonZhangPaymentDetails(List<FormDataMasterBean> masterList, String un_) {
		// 凭证明细
		List<Details> detailsList = new ArrayList<>();
		try {
			for (int i = 0; i < masterList.size(); i++) {
				FormDataMasterBean master = masterList.get(i);
				String costItem = CAP4FormFieldUtils.getFormFieldVal(master, un_ + "费用项",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
				// 借：库存商品 借方科目
				Details inventoryGoodsDetails = new Details();
				// 原币贷方金额
				inventoryGoodsDetails.setCreditamount("0");
				// 如果没有外币业务情况可以跟creditamount传输一样值
				inventoryGoodsDetails.setLocalcreditamount("0");
				// 原币借方金额
				inventoryGoodsDetails.setDebitamount(CAP4FormFieldUtils.getFormFieldVal(master, un_ + "4.1本次支付金额",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				// 如果没有外币业务情况可以传debitamount一样
				inventoryGoodsDetails.setLocaldebitamount(CAP4FormFieldUtils.getFormFieldVal(master, un_ + "4.1本次支付金额",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				// 分录行也可以理解为行号从1开始排
				inventoryGoodsDetails.setDetailindex(no.toString());
				inventoryGoodsDetails.setExplanation(CAP4FormFieldUtils.getFormFieldVal(master, un_ + "借方摘要",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				// 科目
				if ("管理费用".equals(costItem)) {
					inventoryGoodsDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(master,
							un_ + "财务管理U8科目代码", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				} else {
					inventoryGoodsDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(master,
							un_ + "财务销售U8科目代码", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				}
				String checkAss = CAP4FormFieldUtils.getFormFieldVal(master, un_ + "是否有辅助核算",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
				if ("是".equals(checkAss)) {
					// 辅助科目*********用的是自定义字段--"门店"
					ParamAss ass = new ParamAss();
					ass.setChecktypecode("2");
					ass.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, un_ + "部门编码-关联",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
					List<ParamAss> assList = new ArrayList<>();
					assList.add(ass);
					inventoryGoodsDetails.setAss(assList);
				}
				
				detailsList.add(inventoryGoodsDetails);
				
				no++;
			}
		} catch (Exception e) {
			log.error("付款凭证生成事件异常", e);
		}
		return detailsList;
	}

	/**
	 * @param form
	 * @param daiList 
	 * @param transDetails 
	 * @param expenseDetails 
	 * @param paymentDetails 
	 * @MethodName: getPaymentVoucherParams
	 * @Description: 获取总账头部凭证参数
	 * @CreateDate: 2025/9/2 14:09
	 * @Author: CheungHowrAn
	 * @Param master
	 * @Param formBean
	 * @Return List<Details>
	 */
	public VoucherParams getZonzhangVoucher(String id, FormBean form, List<Details> paymentDetails, List<Details> expenseDetails, List<Details> transDetails, List<Details> daiList) {
		VoucherParams voucherParams = new VoucherParams();
		List<Details> List1 = Stream.concat(paymentDetails.stream(), expenseDetails.stream()).collect(Collectors.toList());
		List<Details> List2 = Stream.concat(transDetails.stream(), daiList.stream()).collect(Collectors.toList());
		List<Details> List3 = Stream.concat(List1.stream(), List2.stream()).collect(Collectors.toList());
		try {
			FormDataMasterBean dataMasterBean = formApi4Cap4.getDataMasterBeanById(Long.valueOf(id), form, null);
			String billType = CAP4FormKit.getFieldStrValue(dataMasterBean, "单据类型");
			String un_ = "";// 前缀
			if ("付款单".equals(billType)) {
				un_ = "付款单-";
			} else if ("费用报销单".equals(billType)) {
				un_ = "费用-";
			} else if ("差旅报销单".equals(billType)) {
				un_ = "差旅费-";
			}

			List<Voucher> vouchers = new ArrayList<>();
			Voucher voucher = new Voucher();
			// 摘要信息 3.12付款事由
//            String explanation = CAP4FormFieldUtils.getFormFieldVal(master, "3.12付款事由", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			String explanation = "总账核算凭证";
			log.info("总账摘要:" + explanation);
			voucher.setExplanation(explanation);
			// 传业务发生具体公司（业务单元/组织）编码 todo 字段确认
			String pkCorp = CAP4FormFieldUtils.getFormFieldVal(dataMasterBean, un_ + "总账公司编码",
					CAP4FormFieldUtils.FormFieldValType.SHOW_CODE);
			log.info("公司编码-关联:" + pkCorp);
			voucher.setPk_corp(pkCorp);
			// 传业务发生具体公司（业务单元/组织）账簿编码 todo 字段确认
			String accountBook = CAP4FormFieldUtils.getFormFieldVal(dataMasterBean, un_ + "总账账簿编码",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			log.info("账簿信息:" + accountBook);
			voucher.setPk_glorgbook(accountBook);
			// 制单人（对应u8c用户档案的用户编码） todo 字段确认
			voucher.setPk_prepared("18941149858");//OA制单的手机号码
			String preparedDate = DateUtil.getDate();
			log.info("本月末日期:" + preparedDate);
			voucher.setPrepareddate(preparedDate);
			
			voucher.setDetails(List3);
			vouchers.add(voucher);
			voucherParams.setVoucher(vouchers);
		} catch (Exception e) {
			log.error("总账凭证头部参数组装异常", e);
		}
		return voucherParams;
	}

	/**
	 * @MethodName: dataMerge
	 * @Description: 数据合并
	 * @CreateDate: 2025/9/4 22:51
	 * @Author: CheungHowrAn
	 * @Param paramsList
	 * @Return VoucherParams
	 */
	public VoucherParams dataMerge(List<VoucherParams> paramsList) {

		VoucherParams voucherParams = new VoucherParams();
		try {
			// 用于存储按 pk_accsubj 分组的 Details
			Map<String, Details> detailMap = new LinkedHashMap<>();
			// 遍历所有 VoucherParams 对象
			for (VoucherParams params : paramsList) {
				List<Details> detailsList = params.getVoucher().get(0).getDetails(); // 假设每个 VoucherParams 只有一个 Voucher
				if (detailsList != null) {
					for (Details detail : detailsList) {
						String pkAccsubj = detail.getPk_accsubj();
						if (detailMap.containsKey(pkAccsubj)) {
							Details existingDetail = detailMap.get(pkAccsubj);
							// 合并借方和贷方金额（字符串转为数值处理）
							double existingDebit = Double.parseDouble(existingDetail.getDebitamount());
							double existingCredit = Double.parseDouble(existingDetail.getCreditamount());
							double newDebit = Double.parseDouble(detail.getDebitamount());
							double newCredit = Double.parseDouble(detail.getCreditamount());

							existingDetail.setDebitamount(String.valueOf(existingDebit + newDebit));
							existingDetail.setCreditamount(String.valueOf(existingCredit + newCredit));
						} else {
							detailMap.put(pkAccsubj, detail);
						}
					}
				}
			}

			// 重新设置 detailindex
			int index = 1;
			List<Details> mergedDetails = new ArrayList<>();
			for (Details detail : detailMap.values()) {
				detail.setDetailindex(String.valueOf(index++));
				mergedDetails.add(detail);
			}

			// 构造 Voucher 和 VoucherParams
			Voucher voucher = new Voucher();
			voucher.setDetails(mergedDetails);

			if (!paramsList.isEmpty()) {
				VoucherParams firstParam = paramsList.get(0);
				voucher.setExplanation(firstParam.getVoucher().get(0).getExplanation());
				voucher.setPk_corp(firstParam.getVoucher().get(0).getPk_corp());
				voucher.setPk_glorgbook(firstParam.getVoucher().get(0).getPk_glorgbook());
				voucher.setPk_prepared(firstParam.getVoucher().get(0).getPk_prepared());
				voucher.setPrepareddate(firstParam.getVoucher().get(0).getPrepareddate());
			}
			voucherParams.setVoucher(Arrays.asList(voucher));

		} catch (Exception e) {
			log.error("合并凭证参数失败", e);
		}
		return voucherParams;
	}

	/**
	 * @MethodName: getPaymentVoucherParams
	 * @Description: 获取付款凭证参数
	 * @CreateDate: 2025/9/2 14:09
	 * @Author: CheungHowrAn
	 * @Param master
	 * @Param formBean
	 * @Return List<Details>
	 */
	public VoucherParams getPaymentVoucherParams(FormDataMasterBean master, FormBean formBean) {
		VoucherParams voucherParams = new VoucherParams();
		try {
			List<Voucher> vouchers = new ArrayList<>();
			Voucher voucher = new Voucher();
			// 摘要信息 3.12付款事由
			String explanation = CAP4FormFieldUtils.getFormFieldVal(master, "3.12付款事由",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			log.info("摘要信息  3.12付款事由:" + explanation);
			voucher.setExplanation(explanation);
			// 传业务发生具体公司（业务单元/组织）编码 todo 字段确认
			String pkCorp = CAP4FormFieldUtils.getFormFieldVal(master, "公司编码-关联",
					CAP4FormFieldUtils.FormFieldValType.SHOW_CODE);
			log.info("公司编码-关联:" + pkCorp);
			voucher.setPk_corp(pkCorp);
			// 传业务发生具体公司（业务单元/组织）账簿编码 todo 字段确认
			String accountBook = CAP4FormFieldUtils.getFormFieldVal(master, "账簿编码",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			log.info("账簿信息:" + accountBook);
			voucher.setPk_glorgbook(accountBook);
			// 制单人（对应u8c用户档案的用户编码） todo 字段确认
			String prepared = CAP4FormFieldUtils.getFormFieldVal(master, "负责人-关联",
					CAP4FormFieldUtils.FormFieldValType.SHOW_CODE);
			log.info("负责人-关联:" + prepared);
			voucher.setPk_prepared(prepared);
			// 制单日期 todo 字段确认
			// String preparedDate = CAP4FormFieldUtils.getFormFieldVal(master, "本月末日期",
			// CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			String costItem = CAP4FormFieldUtils.getFormFieldVal(master, "费用项",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			log.info("费用项：" + costItem);
			String preparedDate = DateUtil.getDate();
			log.info("本月末日期:" + preparedDate);
			voucher.setPrepareddate(preparedDate);

			Integer detailIndex = 1;
			// 凭证明细
			List<Details> detailsList = new ArrayList<>();
//            List<FormDataSubBean> subData = CAP4FormKit.getSubBeans(master, "付款明细");
//            for (int i = 0; i < subData.size(); i++) {
//            	String whsje = CAP4FormKit.getFieldStrValue(master, "未含税金额");
// 	            log.info("未含税金额：" + whsje);
//	            String taxAmount = CAP4FormFieldUtils.getFormFieldVal(master, "税额", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
//	            log.info("税额：" + taxAmount);
			// 借：库存商品 借方科目
			Details inventoryGoodsDetails = new Details();
			// 原币贷方金额
			inventoryGoodsDetails.setCreditamount("0");
			// 如果没有外币业务情况可以跟creditamount传输一样值
			inventoryGoodsDetails.setLocalcreditamount("0");
			// 原币借方金额
			inventoryGoodsDetails.setDebitamount(CAP4FormFieldUtils.getFormFieldVal(master, "4.1本次支付金额",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 如果没有外币业务情况可以传debitamount一样
			inventoryGoodsDetails.setLocaldebitamount(CAP4FormFieldUtils.getFormFieldVal(master, "4.1本次支付金额",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 分录行也可以理解为行号从1开始排
			inventoryGoodsDetails.setDetailindex(detailIndex.toString());
			inventoryGoodsDetails.setExplanation(CAP4FormFieldUtils.getFormFieldVal(master, "3.12付款事由",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			inventoryGoodsDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(master, "税务销售科目编码-借",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
//	            if ("管理费用".equals(costItem)) {
//	                //摘要信息
//	                inventoryGoodsDetails.setExplanation(CAP4FormFieldUtils.getFormFieldVal(subData.get(i), "税务管理科目名称-借", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
//	                inventoryGoodsDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(subData.get(i), "税务管理科目编码-借", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
//	            } else {
//	                //摘要信息
//	                inventoryGoodsDetails.setExplanation(CAP4FormFieldUtils.getFormFieldVal(subData.get(i), "税务销售科目名称-借", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
//	                inventoryGoodsDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(subData.get(i), "税务销售科目编码-借", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
//	            }

			// 辅助科目*********用的是自定义字段--"门店"
			ParamAss ass = new ParamAss();
			ass.setChecktypecode("73");// 73 客商辅助核算
			ass.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "收款单位编码",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			List<ParamAss> assList = new ArrayList<>();
			assList.add(ass);
			inventoryGoodsDetails.setAss(assList);

			detailsList.add(inventoryGoodsDetails);
			detailIndex++;

//	            if (Strings.isNotBlank(taxAmount) && new BigDecimal(taxAmount).compareTo(BigDecimal.ZERO) > 0) {
//	            	//借：应交税费 — 应交增值税(进项税额)  		3.9税额
//	                Details taxesPayableDetails = new Details();
//	                //原币贷方金额
//	                taxesPayableDetails.setCreditamount("0");
//	                //如果没有外币业务情况可以跟creditamount传输一样值
//	                taxesPayableDetails.setLocalcreditamount("0");
//	                //原币借方金额
//	                taxesPayableDetails.setDebitamount(taxAmount);
//	                //如果没有外币业务情况可以传debitamount一样
//	                taxesPayableDetails.setLocaldebitamount(taxAmount);
//	                //分录行也可以理解为行号从1开始排
//	                taxesPayableDetails.setDetailindex(detailIndex.toString());
//	                //摘要信息
//	                taxesPayableDetails.setExplanation("应交税费-应交增值税-进项税额");
//	                //科目代码  todo 应交税费-应交增值税-进项税额
//	                taxesPayableDetails.setPk_accsubj("222111");
//	                detailsList.add(taxesPayableDetails);
//	                detailIndex++;
//	            }
//            } 

			// 贷：银行存款 贷方金额
			Details bankDepositDetails = new Details();
			// 原币贷方金额
			bankDepositDetails.setCreditamount(CAP4FormFieldUtils.getFormFieldVal(master, "4.1本次支付金额",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 如果没有外币业务情况可以跟creditamount传输一样值
			bankDepositDetails.setLocalcreditamount(CAP4FormFieldUtils.getFormFieldVal(master, "4.1本次支付金额",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 原币借方金额
			bankDepositDetails.setDebitamount("0");
			// 如果没有外币业务情况可以传debitamount一样
			bankDepositDetails.setLocaldebitamount("0");
			// 分录行也可以理解为行号从1开始排
			bankDepositDetails.setDetailindex(detailIndex.toString());
			// 摘要信息
			bankDepositDetails.setExplanation(CAP4FormFieldUtils.getFormFieldVal(master, "3.12付款事由",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			log.info("costItem==" + costItem);

			// 科目代码
			if ("管理费用".equals(costItem)) {
				bankDepositDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			} else {
				bankDepositDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			}
			// 辅助科目*********
			ParamAss bankDepositAss = new ParamAss();
			if ("100201".equals(
					CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME))
					|| "100202".equals(CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME))) {
				bankDepositAss.setChecktypecode("96");// 银行
				bankDepositAss.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "付款银行账号",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				List<ParamAss> bankDepositAssList = new ArrayList<>();
				bankDepositAssList.add(bankDepositAss);
				bankDepositDetails.setAss(bankDepositAssList);
			} else if ("224102".equals(CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME))) {
				bankDepositAss.setChecktypecode("73");// 73 客商辅助核算
				bankDepositAss.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "收款单位编码",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				List<ParamAss> bankDepositAssList = new ArrayList<>();
				bankDepositAssList.add(bankDepositAss);
				bankDepositDetails.setAss(bankDepositAssList);
			}

			detailsList.add(bankDepositDetails);
			detailIndex++;

			voucher.setDetails(detailsList);
			vouchers.add(voucher);
			voucherParams.setVoucher(vouchers);
		} catch (Exception e) {
			log.error("付款凭证生成事件异常", e);
		}
		return voucherParams;
	}

	/**
	 * @MethodName: getExpenseVoucherParams
	 * @Description: 费用报销--获取报销凭证参数
	 * @CreateDate: 2025/9/2 14:09
	 * @Author: CheungHowrAn
	 * @Param master
	 * @Param formBean
	 * @Return VoucherParams
	 */
	public VoucherParams getExpenseVoucherParams(FormDataMasterBean master, FormBean formBean) {
		VoucherParams voucherParams = new VoucherParams();
		try {
			List<Voucher> vouchers = new ArrayList<>();
			Voucher voucher = new Voucher();
			// 摘要信息
			String explanation = CAP4FormFieldUtils.getFormFieldVal(master, "3.16备注",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			log.info("摘要信息(3.16备注):" + explanation);
			voucher.setExplanation(explanation);
			// 传业务发生具体公司（业务单元/组织）编码 todo 字段确认
			String pkCorp = CAP4FormFieldUtils.getFormFieldVal(master, "公司编码-关联",
					CAP4FormFieldUtils.FormFieldValType.SHOW_CODE);
			log.info("公司编码-关联:" + pkCorp);
			voucher.setPk_corp(pkCorp);
			// 传业务发生具体公司（业务单元/组织）账簿编码 todo 字段确认
			String accountBook = CAP4FormFieldUtils.getFormFieldVal(master, "账簿编码",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			log.info("账簿信息:" + accountBook);
			voucher.setPk_glorgbook(accountBook);
			// 制单人（对应u8c用户档案的用户编码） todo 字段确认
			String prepared = CAP4FormFieldUtils.getFormFieldVal(master, "负责人-关联",
					CAP4FormFieldUtils.FormFieldValType.SHOW_CODE);
			log.info("负责人-关联:" + prepared);
			voucher.setPk_prepared(prepared);
			// 制单日期 todo 字段确认
			// String preparedDate = CAP4FormFieldUtils.getFormFieldVal(master, "本月末日期",
			// CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			String preparedDate = DateUtil.getDate();
			log.info("本月末日期:" + preparedDate);
			voucher.setPrepareddate(preparedDate);

			// 凭证明细
			List<Details> detailsList = getExpenseVoucherDetailsList(master, formBean);
			voucher.setDetails(detailsList);
			vouchers.add(voucher);
			voucherParams.setVoucher(vouchers);
		} catch (Exception e) {
			log.error("报销凭证生成事件异常", e);
		}
		return voucherParams;
	}

	/**
	 * @MethodName: getExpenseVoucherDetailsList
	 * @Description: 费用报销--获取报销凭证分录参数
	 * @CreateDate: 2025/9/2 14:09
	 * @Author: CheungHowrAn
	 * @Param master
	 * @Param formBean
	 * @Return List<Details>
	 */
	private List<Details> getExpenseVoucherDetailsList(FormDataMasterBean master, FormBean formBean) {
		List<Details> detailsList = new ArrayList<>();
		try {
			String tableName = formBean.getFieldBeanByDisplay("借方科目").getOwnerTableName();
			List<FormDataSubBean> subData = master.getSubData(tableName);
			String costItem = CAP4FormFieldUtils.getFormFieldVal(master, "费用项",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			Integer detailIndex = 1;
			for (FormDataSubBean subBean : subData) {
				String taxRate = CAP4FormFieldUtils.getFormFieldVal(subBean, "税额",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
				log.info("税额=====" + taxRate);

				// 无借款 报销5000
				// 借：管理费用 — 费用费 4,716.98
				Details travelExpenseDetails = new Details();
				// 原币贷方金额
				travelExpenseDetails.setCreditamount("0");
				// 如果没有外币业务情况可以跟creditamount传输一样值
				travelExpenseDetails.setLocalcreditamount("0");
				// 原币借方金额
				travelExpenseDetails.setDebitamount(CAP4FormFieldUtils.getFormFieldVal(subBean, "L1.7未含税金额",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				// 如果没有外币业务情况可以传debitamount一样
				travelExpenseDetails.setLocaldebitamount(CAP4FormFieldUtils.getFormFieldVal(subBean, "L1.7未含税金额",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				// 分录行也可以理解为行号从1开始排
				travelExpenseDetails.setDetailindex(detailIndex.toString());
				// 摘要信息
				travelExpenseDetails.setExplanation(CAP4FormFieldUtils.getFormFieldVal(subBean, "借方摘要",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				// 科目代码
				if ("管理费用".equals(costItem)) {
					travelExpenseDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(subBean, "税务管理科目编码-借",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				} else {
					travelExpenseDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(subBean, "税务销售科目编码-借",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				}
				// 辅助科目*********用的是自定义字段--"门店"
				ParamAss ass = new ParamAss();
				ass.setChecktypecode("DCW001");
				ass.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "门店编码",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				List<ParamAss> assList = new ArrayList<>();
				assList.add(ass);
				travelExpenseDetails.setAss(assList);

				detailsList.add(travelExpenseDetails);
				detailIndex++;

				// 税金
				if (Strings.isNotBlank(taxRate) && new BigDecimal(taxRate).compareTo(BigDecimal.ZERO) > 0) {
					// 借：应交税费 — 应交增值税(进项税额) 283.02
					Details taxesPayableDetails = new Details();
					// 原币贷方金额
					taxesPayableDetails.setCreditamount("0");
					// 如果没有外币业务情况可以跟creditamount传输一样值
					taxesPayableDetails.setLocalcreditamount("0");
					// 原币借方金额
					taxesPayableDetails.setDebitamount(CAP4FormFieldUtils.getFormFieldVal(subBean, "税额",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
					// 如果没有外币业务情况可以传debitamount一样
					taxesPayableDetails.setLocaldebitamount(CAP4FormFieldUtils.getFormFieldVal(subBean, "税额",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
					// 分录行也可以理解为行号从1开始排
					taxesPayableDetails.setDetailindex(detailIndex.toString());
					// 摘要信息
					taxesPayableDetails.setExplanation("应交税费-应交增值税-进项税额");
					// 科目代码 22210101 应交税金-应交增值税-待认证进项税额222111
					taxesPayableDetails.setPk_accsubj("222111");
					detailsList.add(taxesPayableDetails);
					detailIndex++;
				}
			}
			String skCode = CAP4FormFieldUtils.getFormFieldVal(master, "收款单位编码",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			if (Strings.isNotBlank(skCode)) {
				String je = CAP4FormFieldUtils.getFormFieldVal(master, "3.6本次支付金额",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
				// 借：应交税费 — 应交增值税(进项税额) 283.02
				Details taxesPayableDetails = new Details();
				// 原币贷方金额
				taxesPayableDetails.setCreditamount("0");
				// 如果没有外币业务情况可以跟creditamount传输一样值
				taxesPayableDetails.setLocalcreditamount("0");
				// 原币借方金额
				taxesPayableDetails.setDebitamount(je);
				// 如果没有外币业务情况可以传debitamount一样
				taxesPayableDetails.setLocaldebitamount(je);
				// 分录行也可以理解为行号从1开始排
				taxesPayableDetails.setDetailindex(detailIndex.toString());
				// 摘要信息
				taxesPayableDetails.setExplanation("应交税费-应交增值税-进项税额");
				// 科目代码 22210101 应交税金-应交增值税-待认证进项税额222111
				taxesPayableDetails.setPk_accsubj("224102");

				ParamAss zomDepositAss = new ParamAss();
				zomDepositAss.setChecktypecode("73");// 73 客商辅助核算
				zomDepositAss.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "收款单位编码",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				List<ParamAss> zomDepositAssList = new ArrayList<>();
				zomDepositAssList.add(zomDepositAss);
				taxesPayableDetails.setAss(zomDepositAssList);
				detailsList.add(taxesPayableDetails);
				detailIndex++;

				// 贷：库存现金 / 银行存款 5,000.00
				Details bankDepositDetails = new Details();
				// 原币贷方金额
				bankDepositDetails.setCreditamount(je);
				// 如果没有外币业务情况可以跟creditamount传输一样值
				bankDepositDetails.setLocalcreditamount(je);
				// 原币借方金额
				bankDepositDetails.setDebitamount("0");
				// 如果没有外币业务情况可以传debitamount一样
				bankDepositDetails.setLocaldebitamount("0");
				// 分录行也可以理解为行号从1开始排
				bankDepositDetails.setDetailindex(detailIndex.toString());
				// 摘要信息
				bankDepositDetails.setExplanation(CAP4FormFieldUtils.getFormFieldVal(master, "贷方摘要",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				// 科目代码
				bankDepositDetails.setPk_accsubj("224102");
				// 辅助科目*********
				ParamAss bankDepositAss = new ParamAss();
				bankDepositAss.setChecktypecode("73");// 73 客商辅助核算
				bankDepositAss.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "收款单位编码",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				List<ParamAss> bankDepositAssList = new ArrayList<>();
				bankDepositAssList.add(bankDepositAss);
				bankDepositDetails.setAss(bankDepositAssList);

				detailsList.add(bankDepositDetails);
				detailIndex++;
			}

			// 贷：库存现金 / 银行存款 5,000.00
			Details bankDepositDetails = new Details();
			// 原币贷方金额
			bankDepositDetails.setCreditamount(CAP4FormFieldUtils.getFormFieldVal(master, "3.6本次支付金额",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 如果没有外币业务情况可以跟creditamount传输一样值
			bankDepositDetails.setLocalcreditamount(CAP4FormFieldUtils.getFormFieldVal(master, "3.6本次支付金额",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 原币借方金额
			bankDepositDetails.setDebitamount("0");
			// 如果没有外币业务情况可以传debitamount一样
			bankDepositDetails.setLocaldebitamount("0");
			// 分录行也可以理解为行号从1开始排
			bankDepositDetails.setDetailindex(detailIndex.toString());
			// 摘要信息
			bankDepositDetails.setExplanation(
					CAP4FormFieldUtils.getFormFieldVal(master, "贷方摘要", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 科目代码
			if ("管理费用".equals(costItem)) {
				bankDepositDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			} else {
				bankDepositDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			}
			// 辅助科目*********
			ParamAss bankDepositAss = new ParamAss();
			if ("100201".equals(
					CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME))
					|| "100202".equals(CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码",
							CAP4FormFieldUtils.FormFieldValType.SHOW_NAME))) {
				bankDepositAss.setChecktypecode("96");// 银行
				bankDepositAss.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "付款银行账号",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				List<ParamAss> bankDepositAssList = new ArrayList<>();
				bankDepositAssList.add(bankDepositAss);
				bankDepositDetails.setAss(bankDepositAssList);
			} else if ("224102".equals(CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME))) {
				bankDepositAss.setChecktypecode("73");// 73 客商辅助核算
				bankDepositAss.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "收款单位编码",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				List<ParamAss> bankDepositAssList = new ArrayList<>();
				bankDepositAssList.add(bankDepositAss);
				bankDepositDetails.setAss(bankDepositAssList);
			}
			detailsList.add(bankDepositDetails);
			detailIndex++;
		} catch (Exception e) {
			log.error("费用凭证生成事件异常", e);
		}
		return detailsList;
	}

	/**
	 * @MethodName: getTransferVoucherParams
	 * @Description: 获取差旅凭证参数
	 * @CreateDate: 2025/9/2 14:10
	 * @Author: CheungHowrAn
	 * @Param master
	 * @Return VoucherParams
	 */
	public VoucherParams getTransferVoucherParams(FormDataMasterBean master) {
		VoucherParams voucherParams = new VoucherParams();
		try {
			List<Voucher> vouchers = new ArrayList<>();
			Voucher voucher = new Voucher();
			// 摘要信息 3.12付款事由
			String explanation = CAP4FormFieldUtils.getFormFieldVal(master, "摘要",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			log.info("摘要信息:" + explanation);
			voucher.setExplanation(explanation);
			// 传业务发生具体公司（业务单元/组织）编码 todo 字段确认
			String pkCorp = CAP4FormFieldUtils.getFormFieldVal(master, "部门编码-关联",
					CAP4FormFieldUtils.FormFieldValType.SHOW_CODE);
			log.info("部门编码-关联:" + pkCorp);
			voucher.setPk_corp(pkCorp);
			// 传业务发生具体公司（业务单元/组织）账簿编码 todo 字段确认
			String accountBook = CAP4FormFieldUtils.getFormFieldVal(master, "账簿编码",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			log.info("账簿信息:" + accountBook);
			voucher.setPk_glorgbook(accountBook);
			// 制单人（对应u8c用户档案的用户编码） todo 字段确认
			String prepared = CAP4FormFieldUtils.getFormFieldVal(master, "负责人-关联",
					CAP4FormFieldUtils.FormFieldValType.SHOW_CODE);
			log.info("负责人-关联:" + prepared);
			voucher.setPk_prepared(prepared);
			// 制单日期 todo 字段确认
			// String preparedDate = CAP4FormFieldUtils.getFormFieldVal(master, "本月末日期",
			// CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			String preparedDate = DateUtil.getDate();
			log.info("本月末日期:" + preparedDate);
			voucher.setPrepareddate(preparedDate);

			String costItem = CAP4FormFieldUtils.getFormFieldVal(master, "费用项",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			String taxAmount = CAP4FormFieldUtils.getFormFieldVal(master, "3.3税额合计",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			Integer detailIndex = 1;
			// 凭证明细
			List<Details> detailsList = new ArrayList<>();
//            借：库存商品     	借方科目      				3.8未税金额
			Details inventoryGoodsDetails = new Details();
			// 原币贷方金额
			inventoryGoodsDetails.setCreditamount("0");
			// 如果没有外币业务情况可以跟creditamount传输一样值
			inventoryGoodsDetails.setLocalcreditamount("0");
			// 原币借方金额
			inventoryGoodsDetails.setDebitamount(CAP4FormFieldUtils.getFormFieldVal(master, "3.2未税金额合计",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 如果没有外币业务情况可以传debitamount一样
			inventoryGoodsDetails.setLocaldebitamount(CAP4FormFieldUtils.getFormFieldVal(master, "3.2未税金额合计",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 分录行也可以理解为行号从1开始排
			inventoryGoodsDetails.setDetailindex(detailIndex.toString());

			if ("管理费用".equals(costItem)) {
				// 摘要信息
				inventoryGoodsDetails.setExplanation(CAP4FormFieldUtils.getFormFieldVal(master, "税务管理科目名称-借",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				inventoryGoodsDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(master, "税务管理科目编码-借",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			} else {
				// 摘要信息
				inventoryGoodsDetails.setExplanation(CAP4FormFieldUtils.getFormFieldVal(master, "税务销售科目名称-借",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				inventoryGoodsDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(master, "税务销售科目编码-借",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			}
			// 科目代码
			detailsList.add(inventoryGoodsDetails);
			detailIndex++;

			if (Strings.isNotBlank(taxAmount) && new BigDecimal(taxAmount).compareTo(BigDecimal.ZERO) > 0) {
//            借：应交税费 — 应交增值税(进项税额)  		3.9税额
				Details taxesPayableDetails = new Details();
				// 原币贷方金额
				taxesPayableDetails.setCreditamount("0");
				// 如果没有外币业务情况可以跟creditamount传输一样值
				taxesPayableDetails.setLocalcreditamount("0");
				// 原币借方金额
				taxesPayableDetails.setDebitamount(CAP4FormFieldUtils.getFormFieldVal(master, "3.3税额合计",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				// 如果没有外币业务情况可以传debitamount一样
				taxesPayableDetails.setLocaldebitamount(CAP4FormFieldUtils.getFormFieldVal(master, "3.3税额合计",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				// 分录行也可以理解为行号从1开始排
				taxesPayableDetails.setDetailindex(detailIndex.toString());
				// 摘要信息
				taxesPayableDetails.setExplanation("应交税费-应交增值税-进项税额");
				// 科目代码 todo 字段确认 22210101 应交税费-应交增值税-进项税额
				taxesPayableDetails.setPk_accsubj("22210101");
				detailsList.add(taxesPayableDetails);
				detailIndex++;
			}

//            贷：银行存款     		贷方金额
			Details bankDepositDetails = new Details();
			// 原币贷方金额
			bankDepositDetails.setCreditamount(CAP4FormFieldUtils.getFormFieldVal(master, "3.1费用金额合计",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 如果没有外币业务情况可以跟creditamount传输一样值
			bankDepositDetails.setLocalcreditamount(CAP4FormFieldUtils.getFormFieldVal(master, "3.1费用金额合计",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 原币借方金额
			bankDepositDetails.setDebitamount("0");
			// 如果没有外币业务情况可以传debitamount一样
			bankDepositDetails.setLocaldebitamount("0");
			// 分录行也可以理解为行号从1开始排
			bankDepositDetails.setDetailindex(detailIndex.toString());

			if ("管理费用".equals(costItem)) {
				// 摘要信息
				bankDepositDetails.setExplanation(CAP4FormFieldUtils.getFormFieldVal(master, "税务管理科目名称-贷",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				// 科目代码 todo 字段确认
				bankDepositDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(master, "税务管理科目编码-贷",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			} else {
				// 摘要信息
				bankDepositDetails.setExplanation(CAP4FormFieldUtils.getFormFieldVal(master, "税务管理科目名称-贷",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				// 科目代码 todo 字段确认
				bankDepositDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(master, "税务销售科目编码-贷",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			}
			// todo 银行存款是否需要传现金流 传的话编码怎么获取
			detailsList.add(bankDepositDetails);

			voucher.setDetails(detailsList);
			vouchers.add(voucher);
			voucherParams.setVoucher(vouchers);
		} catch (Exception e) {
			log.error("付款凭证生成事件异常", e);
		}
		return voucherParams;
	}

	/**
	 * @MethodName: getTransferVoucherParams
	 * @Description: 获取差旅凭证参数
	 * @CreateDate: 2025/9/2 14:10
	 * @Author: CheungHowrAn
	 * @Param master
	 * @Param formBean
	 * @Return VoucherParams
	 */
	public VoucherParams getTransferVoucherParams(FormDataMasterBean master, FormBean formBean) {
		VoucherParams voucherParams = new VoucherParams();
		try {
			List<Voucher> vouchers = new ArrayList<>();
			Voucher voucher = new Voucher();
			// 摘要信息
			voucher.setExplanation(
					CAP4FormFieldUtils.getFormFieldVal(master, "摘要", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 传业务发生具体公司（业务单元/组织）编码 todo 字段确认
			voucher.setPk_corp(CAP4FormFieldUtils.getFormFieldVal(master, "公司编码-关联",
					CAP4FormFieldUtils.FormFieldValType.SHOW_CODE));
			// 传业务发生具体公司（业务单元/组织）账簿编码 todo 字段确认
			voucher.setPk_glorgbook(
					CAP4FormFieldUtils.getFormFieldVal(master, "账簿编码", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 制单人（对应u8c用户档案的用户编码） todo 字段确认
			voucher.setPk_prepared(CAP4FormFieldUtils.getFormFieldVal(master, "负责人-关联",
					CAP4FormFieldUtils.FormFieldValType.SHOW_CODE));
			// 制单日期 todo 字段确认
			voucher.setPrepareddate(
					CAP4FormFieldUtils.getFormFieldVal(master, "本月末日期", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 制单日期
			voucher.setPrepareddate(DateUtil.getDate());
			// 凭证明细
			List<Details> detailsList = getTransferVoucherDetailsList(master, formBean);
			voucher.setDetails(detailsList);
			vouchers.add(voucher);
			voucherParams.setVoucher(vouchers);

		} catch (Exception e) {
			log.error("差旅凭证生成事件异常", e);
		}
		return voucherParams;
	}

	/**
	 * @MethodName: getTransferVoucherDetailsList
	 * @Description: 获取差旅凭证分录参数
	 * @CreateDate: 2025/9/2 14:11
	 * @Author: CheungHowrAn
	 * @Param master
	 * @Param formBean
	 * @Return List<Details>
	 */
	private List<Details> getTransferVoucherDetailsList(FormDataMasterBean master, FormBean formBean) {
		List<Details> detailsList = new ArrayList<>();
		try {
			String costItem = CAP4FormFieldUtils.getFormFieldVal(master, "费用项",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME);
			log.info("费用项：" + costItem);
			Integer detailIndex = 1;

			// 交通费合计
			String jtwshj = CAP4FormKit.getFieldStrValue(master, "交通未税金额合计");
			String jtsehj = CAP4FormKit.getFieldStrValue(master, "交通税额合计");
			String bz = CAP4FormKit.getFieldStrValue(master, "3.8备注");
			if (Strings.isNotBlank(jtwshj) && new BigDecimal(jtwshj).compareTo(BigDecimal.ZERO) > 0) {
				// 借：管理费用 — 费用费
				Details travelExpenseDetails = new Details();
				// 原币贷方金额
				travelExpenseDetails.setCreditamount("0");
				// 如果没有外币业务情况可以跟creditamount传输一样值
				travelExpenseDetails.setLocalcreditamount("0");
				// 原币借方金额
				travelExpenseDetails.setDebitamount(jtwshj);
				// 如果没有外币业务情况可以传debitamount一样
				travelExpenseDetails.setLocaldebitamount(jtwshj);
				// 分录行也可以理解为行号从1开始排
				travelExpenseDetails.setDetailindex(detailIndex.toString());
				// 摘要信息
				travelExpenseDetails.setExplanation(bz);
				// 科目代码--城市间交通费
				if ("管理费用".equals(costItem)) {
					travelExpenseDetails.setPk_accsubj("66020501");
				} else {
					travelExpenseDetails.setPk_accsubj("66010301");
				}

				// 辅助科目*********用的是自定义字段--"门店"
				ParamAss ass = new ParamAss();
				ass.setChecktypecode("DCW001");
				ass.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "门店编码",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				List<ParamAss> assList = new ArrayList<>();
				assList.add(ass);
				travelExpenseDetails.setAss(assList);

				detailsList.add(travelExpenseDetails);
				detailIndex++;
			}
			// 住宿费合计
			String zswshj = CAP4FormKit.getFieldStrValue(master, "住宿未税金额合计");
			String zssehj = CAP4FormKit.getFieldStrValue(master, "住宿税额合计");
			if (Strings.isNotBlank(zswshj) && new BigDecimal(zswshj).compareTo(BigDecimal.ZERO) > 0) {
				// 借：管理费用 — 费用费
				Details zhusuExpenseDetails = new Details();
				// 原币贷方金额
				zhusuExpenseDetails.setCreditamount("0");
				// 如果没有外币业务情况可以跟creditamount传输一样值
				zhusuExpenseDetails.setLocalcreditamount("0");
				// 原币借方金额
				zhusuExpenseDetails.setDebitamount(zswshj);
				// 如果没有外币业务情况可以传debitamount一样
				zhusuExpenseDetails.setLocaldebitamount(zswshj);
				// 分录行也可以理解为行号从1开始排
				zhusuExpenseDetails.setDetailindex(detailIndex.toString());
				// 摘要信息
				zhusuExpenseDetails.setExplanation(bz);
				// 科目代码--住宿费
				if ("管理费用".equals(costItem)) {
					zhusuExpenseDetails.setPk_accsubj("66020502");
				} else {
					zhusuExpenseDetails.setPk_accsubj("66010302");
				}

				// 辅助科目*********用的是自定义字段--"门店"
				ParamAss ass = new ParamAss();
				ass.setChecktypecode("DCW001");
				ass.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "门店编码",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				List<ParamAss> assList = new ArrayList<>();
				assList.add(ass);
				zhusuExpenseDetails.setAss(assList);

				detailsList.add(zhusuExpenseDetails);
				detailIndex++;
			}

			// 出差补助合计
			String bzhj = CAP4FormKit.getFieldStrValue(master, "3.5出差补助合计");
			if (Strings.isNotBlank(bzhj) && new BigDecimal(bzhj).compareTo(BigDecimal.ZERO) > 0) {
				// 借：管理费用 — 费用费
				Details buzhuExpenseDetails = new Details();
				// 原币贷方金额
				buzhuExpenseDetails.setCreditamount("0");
				// 如果没有外币业务情况可以跟creditamount传输一样值
				buzhuExpenseDetails.setLocalcreditamount("0");
				// 原币借方金额
				buzhuExpenseDetails.setDebitamount(bzhj);
				// 如果没有外币业务情况可以传debitamount一样
				buzhuExpenseDetails.setLocaldebitamount(bzhj);
				// 分录行也可以理解为行号从1开始排
				buzhuExpenseDetails.setDetailindex(detailIndex.toString());
				// 摘要信息
				buzhuExpenseDetails.setExplanation(bz);
				// 科目代码--综合补贴
				if ("管理费用".equals(costItem)) {
					buzhuExpenseDetails.setPk_accsubj("66020503");
				} else {
					buzhuExpenseDetails.setPk_accsubj("66010303");
				}

				// 辅助科目*********用的是自定义字段--"门店"
				ParamAss ass = new ParamAss();
				ass.setChecktypecode("DCW001");
				ass.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "门店编码",
						CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
				List<ParamAss> assList = new ArrayList<>();
				assList.add(ass);
				buzhuExpenseDetails.setAss(assList);

				detailsList.add(buzhuExpenseDetails);
				detailIndex++;
			}

			// 税额合计
			BigDecimal jttaxRate = new BigDecimal(jtsehj);
			BigDecimal zsRate = new BigDecimal(zssehj);
			BigDecimal taxTotle = jttaxRate.add(zsRate);
			if (taxTotle != null && taxTotle.compareTo(BigDecimal.ZERO) > 0) {
				// 借：应交税费 — 应交增值税(进项税额) 283.02
				Details taxesPayableDetails = new Details();
				// 原币贷方金额
				taxesPayableDetails.setCreditamount("0");
				// 如果没有外币业务情况可以跟creditamount传输一样值
				taxesPayableDetails.setLocalcreditamount("0");
				// 原币借方金额
				taxesPayableDetails.setDebitamount(taxTotle.toString());
				// 如果没有外币业务情况可以传debitamount一样
				taxesPayableDetails.setLocaldebitamount(taxTotle.toString());
				// 分录行也可以理解为行号从1开始排
				taxesPayableDetails.setDetailindex(detailIndex.toString());
				// 摘要信息
				taxesPayableDetails.setExplanation(bz);
				// 科目代码
				taxesPayableDetails.setPk_accsubj("222111");
				detailsList.add(taxesPayableDetails);
				detailIndex++;

			}

			// 贷：库存现金 / 银行存款 5,000.00
			Details bankDepositDetails = new Details();
			// 原币贷方金额
			bankDepositDetails.setCreditamount(CAP4FormFieldUtils.getFormFieldVal(master, "3.13本次支付金额",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 如果没有外币业务情况可以跟creditamount传输一样值
			bankDepositDetails.setLocalcreditamount(CAP4FormFieldUtils.getFormFieldVal(master, "3.13本次支付金额",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 原币借方金额
			bankDepositDetails.setDebitamount("0");
			// 如果没有外币业务情况可以传debitamount一样
			bankDepositDetails.setLocaldebitamount("0");
			// 分录行也可以理解为行号从1开始排
			bankDepositDetails.setDetailindex(detailIndex.toString());
			// 摘要信息
			bankDepositDetails.setExplanation(
					CAP4FormFieldUtils.getFormFieldVal(master, "摘要", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			// 科目代码
			bankDepositDetails.setPk_accsubj(CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));

			// 辅助科目*********
			ParamAss bankDepositAss = new ParamAss();
//            if("100201".equals(CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME)) || "100202".equals(CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME))) {
			bankDepositAss.setChecktypecode("96");// 银行
			bankDepositAss.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "付款银行账号",
					CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
			List<ParamAss> bankDepositAssList = new ArrayList<>();
			bankDepositAssList.add(bankDepositAss);
			bankDepositDetails.setAss(bankDepositAssList);
//            }else if("224102".equals(CAP4FormFieldUtils.getFormFieldVal(master, "贷方科目编码", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME))){
//            	bankDepositAss.setChecktypecode("73");//73	客商辅助核算
//	                bankDepositAss.setCheckvaluecode(CAP4FormFieldUtils.getFormFieldVal(master, "收款单位编码", CAP4FormFieldUtils.FormFieldValType.SHOW_NAME));
//	                List<ParamAss> bankDepositAssList = new ArrayList<>();
//	                bankDepositAssList.add(bankDepositAss);
//	                bankDepositDetails.setAss(bankDepositAssList);
//            }

			detailsList.add(bankDepositDetails);

		} catch (Exception e) {
			log.error("差旅报销单凭证生成事件异常", e);
		}
		return detailsList;
	}
	
	
	public static void main(String[] args) {

			BigDecimal feiyon = BigDecimal.ZERO;
			List<Map<String,String>> zhifufeiyon = new ArrayList<>();
			Map<String,String> map = new HashMap<String, String>();
			map.put("22", "200");
			zhifufeiyon.add(map);
			String zhanghao = "22";
			for (int j = 0; j < zhifufeiyon.size(); j++) {
				if(zhifufeiyon.get(j).containsKey(zhanghao)){
					feiyon = feiyon.add(new BigDecimal(zhifufeiyon.get(j).get(zhanghao)));
					System.out.println("feiyon2=="+feiyon);
				}
			}
			System.out.println("feiyon=="+feiyon);
	}
	
	
}
